using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.DataBinding;
using Sedna.Core.Globalization;
using Sedna.Core.Tiles.Contents;

namespace Sedna.Core.Controls
{
	public class RequiredFieldValidator : System.Web.UI.WebControls.RequiredFieldValidator, IContent, IConfigurationAware
	{

		private IConfiguration configuration;
		private string configurationId;

		private string textKey;
		private string errorMessageKey;

		private String contentPlaceHolderId;

	    protected IDataBinder binder;

		public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		public IConfiguration Configuration
		{
			get { return configuration; }
			set { configuration = value; }
		}

		[Bindable(true), Category("Sedna"), DefaultValue("configuration.default.RequiredFieldValidator")]
		public string ConfigurationId
		{
			get { return configurationId; }
			set { configurationId = value; }
		}

		public string TextKey
		{
			get { return textKey; }
			set { textKey = value; }
		}

		public string ErrorMessageKey
		{
			get { return errorMessageKey; }
			set { errorMessageKey = value; }
		}


		public RequiredFieldValidator()
		{
			configurationId = "configuration.default.RequiredFieldValidator";
		}

		public RequiredFieldValidator(IConfiguration configuration) : this()
		{
			Configuration = configuration;
			BuildControlFromConfiguration();
		}

		protected void BuildControlFromConfiguration()
		{
			ID=Configuration.Id;
			
			TextKey = configuration.TextKey;
            Enabled = configuration.Enabled;
            ValidationGroup = configuration.ValidationGroup;

            IRequiredFieldValidatorConfiguration validatorConfiguration = Configuration as IRequiredFieldValidatorConfiguration;
			if (validatorConfiguration == null)
			{
				return;
			}

            EnableClientScript = validatorConfiguration.EnableClientScript;
            ControlToValidate = validatorConfiguration.ControlToValidate;
			ErrorMessageKey = validatorConfiguration.ErrorMessageKey;
			Display = validatorConfiguration.Display;
            InitialValue = validatorConfiguration.InitialValue;
		}

		protected override void OnInit(EventArgs e)
		{
			if (Configuration == null)
			{
				Configuration = (IConfiguration)SpringContextFactory.GetContext().GetObject(configurationId);
			}
            binder = new DataBinder(this, configuration, ExecutionContext.GetCurrent(ClientID));
            BuildControlFromConfiguration();
			base.OnInit (e);
		}

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Unbind();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Bind();
			Text = configuration.GetLocalizedPostfixLabel(TextKey, Text);
		    ErrorMessage = configuration.GetLocalizedPostfixLabel(ErrorMessageKey, ErrorMessage);
        }

        protected virtual void Bind()
        {
            binder.Bind(new string[] { "Enabled", "ValidationGroup" });
        }

        protected virtual void Unbind()
        {
            binder.Unbind(new string[] { "Enabled", "ValidationGroup" });
        }
    }
}
